//
//  AdvertisingTableViewController.m
//  HotWord
//
//  Created by Rainbow on 1/18/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "AdvertisingTableViewController.h"
#import "AdvertisingDetailsViewController.h"
#import "ServiceHelper.h"
#import "PersistenceData.h"
#import "AdvertisingCell.h"
#import "HotWordAppDelegate.h"

@implementation AdvertisingTableViewController

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];    
    _advertisings = [[[PersistenceData sharedData] getAdvertsings] retain];
    
    
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];    
    
    HotWordAppDelegate *appDelegate= (HotWordAppDelegate *)[UIApplication sharedApplication].delegate;
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDuration:0.2f];
    [appDelegate.MainToolBar viewWithTag:66].alpha=0.0f;
    [appDelegate.MainToolBar viewWithTag:77].alpha=0.0f;
    [UIView setAnimationDidStopSelector:@selector(updateToolBtn)];
    [UIView commitAnimations];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return [_advertisings count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"AdvertisingCell";
    
    AdvertisingCell *cell = (AdvertisingCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    cell.selectionStyle = UITableViewCellSelectionStyleGray;
    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"AdvertisingCell" owner:self options:nil] objectAtIndex:0];
    }
    Advertising *advertising = [_advertisings objectAtIndex:indexPath.row]; 
    
    [cell setAdvertisingForCell:advertising];
    
    return cell;
}

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    Advertising *advertising = [_advertisings objectAtIndex:indexPath.row];
    AdvertisingDetailsViewController *advertisingDetailsViewController = [[[AdvertisingDetailsViewController alloc] initWithAdvertising:advertising] autorelease];
    [self.navigationController pushViewController:advertisingDetailsViewController animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}

- (void)retrunBack
{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)updateToolBtn
{   
    HotWordAppDelegate *appDelegate= (HotWordAppDelegate *)[UIApplication sharedApplication].delegate;
    [[appDelegate.MainToolBar viewWithTag:77]removeFromSuperview];
    [[appDelegate.MainToolBar viewWithTag:66]removeFromSuperview];
    [[appDelegate.MainToolBar viewWithTag:88]removeFromSuperview];
    
    UIButton *backToMenuBtn=[[[UIButton alloc]init] autorelease];

    backToMenuBtn.frame=CGRectMake(-50, 20, 100, 100);
    backToMenuBtn.tag=66;
    [backToMenuBtn addTarget:self action:@selector(retrunBack) forControlEvents:UIControlEventTouchUpInside];
    [backToMenuBtn setImage:[UIImage imageNamed:@"back_arrow.png"] forState:UIControlStateNormal];
    [backToMenuBtn setImage:[UIImage imageNamed:@"back_arrow.png"] forState:UIControlStateHighlighted];
    [backToMenuBtn setBackgroundImage:[UIImage imageNamed:@"tap_light.png"] forState:UIControlStateHighlighted];
    backToMenuBtn.alpha=0.0f; 
    [appDelegate.MainToolBar addSubview:backToMenuBtn];
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDuration:0.5f];
    
    backToMenuBtn.alpha=1.0f; 
    [UIView commitAnimations];

}

- (void)dealloc
{
    [_advertisings release];
    [super dealloc];
}

@end


